//
//  AppDelegate.swift
//  个人微博
//
//  Created by 一介布衣 on 16/2/13.
//  Copyright © 2016年 HUAMANLOU. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        // 注册监听通知
        registerNotification()
        
        // 实例化window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.makeKeyAndVisible()
        
        // 设置根视图控制器
        window?.rootViewController = defaultRootViewController()
        
//        let composeVc = PictureSelectorViewController()
//        let nav = BaseNavigationController(rootViewController:composeVc)
//        window?.rootViewController = nav
        
        // 设置主题色
        setThemeColor()
        
        return true
    }
    
    
    // 注册监听通知的方法
    private func registerNotification() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "AppRootViewController:", name: kChoseRootVc, object: nil)
    }
    
    // 添加@objec消息机制才能够找到该方法
    @objc private func AppRootViewController(notifi:NSNotification) {
        print(notifi)
        print("APPDelegate")
        // 使用三木元算简化代码，也可使用if判断语句
        window?.rootViewController = notifi.object != nil ? MainTabBarController() :WelcomeViewController()
        
    }
    
    /// 根视图的切换
    private func defaultRootViewController()->(UIViewController) {
        // 判断用户是否登录
        if AccountViewModel.sharedAccountViewModel.userLogin {
            return WelcomeViewController()
        }
        
        return MainTabBarController()
    }
    
    
    /// 设置主题颜色
    private func setThemeColor() {
        // 使用appearance()渲染主题色，必须提前设置
        // 必须在控件实例化之前设置控件的渲染色
        UINavigationBar.appearance().tintColor = themeColor
        UITabBar.appearance().tintColor = themeColor
    }
    
    
    // 移除通知，这是编码规范而已
    // 此处的代理对象是一个单例对象，程序退出的时候才会销毁创建的对象，
    // 所以此处不写移除通知的方法也是可以的
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }
    
    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }
    
    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }
    
    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }
    
    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    
    
}

